package guet.dream.category.servlet;

import guet.dream.category.Category;
import guet.dream.category.CategoryDao;
import guet.dream.category.CategoryDaoImpl;

import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Servlet implementation class CreateCategoryServlet
 */
public class CreateCategoryServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
 

	
	public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
	 	CategoryDao categoryDao = new CategoryDaoImpl();
	 	String name = (String) request.getAttribute("name");
	 	String description = (String) request.getAttribute("description");
	 	int  parentId = (Integer) request.getAttribute("parentId");
	 	int categoryCode = categoryDao.findMaxId() + 1;
	 	
	 	Category category = new Category(categoryCode,name,description);
	 	
	 	int flag = categoryDao.save(category, parentId);
	 	
	 	if(flag == 1){
	 		
	 	}else{
	 		throw new ServletException("添加类别失败!");
	 	}
	 	
	}


	public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		
	}

}
